package com.mkono.infra.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mkono.infra.BuildConfig;
import com.mkono.infra.R;
import com.mkono.infra.activity.UserAgreeAvtivity;
import com.mkono.infra.activity.UserPolicyAvtivity;
import com.mkono.infra.activity.Welcome;
import com.mkono.infra.constant.Constant;
import com.mkono.infra.untils.Util;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * Created by huang on 2019/11/2.
 */

public class PersonFragment extends Fragment implements View.OnClickListener {

    //头像相关
    PopupWindow systemInfoPop,shareInfoPop;

    private IWXAPI weixinapi;
    private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //布局文件转成View
        //参数1：Fragment需要加载的布局文件
        //参数2：加载layout的父ViewGroup
        //参数1：是否返回父ViewGroup对象，false为不
        View view = inflater.inflate(R.layout.activity_person,container,false);
        //微信公众号mkono
        RelativeLayout mkono = view.findViewById(R.id.mkono);
        mkono.setOnClickListener(this);
        //联系我们
        RelativeLayout contact_us = view.findViewById(R.id.contact_us);
        contact_us.setOnClickListener(this);
        //分享app
        RelativeLayout share_app =view.findViewById(R.id.share_app);
        share_app.setOnClickListener(this);
        //跳转官网
        RelativeLayout website = view.findViewById(R.id.website);
        website.setOnClickListener(this);
        //跳转用户协议
        RelativeLayout user_agree = view.findViewById(R.id.user_agree);
        user_agree.setOnClickListener(this);
        //跳转隐私策略
        RelativeLayout privacy_policy = view.findViewById(R.id.privacy_policy);
        privacy_policy.setOnClickListener(this);
        weixinapi = WXAPIFactory.createWXAPI(getActivity(), Constant.APP_ID, true);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.mkono:
                if (weixinapi.getWXAppSupportAPI()>= TIMELINE_SUPPORTED_VERSION){

                } else {
                    Toast.makeText(getActivity(),"您的微信版本低或您没有安装微信",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.website:
                Intent intent= new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://www.mkono.cn");
                intent.setData(content_url);
                startActivity(intent);
                break;
            case R.id.contact_us:
                View systemInfo = getLayoutInflater().inflate(R.layout.pop_systeminfo, null);
                systemInfoPop = new PopupWindow(systemInfo,LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                systemInfoPop.setFocusable(true);
                TextView versionName = systemInfo.findViewById(R.id.versionName);
                versionName.setText("V"+BuildConfig.VERSION_NAME);
                systemInfo.setOnClickListener(new View.OnClickListener() {//设置点击消失
                    @Override
                    public void onClick(View view) {
                        dissmisspopwindow();
                    }
                });
                systemInfoPop.setBackgroundDrawable(new ColorDrawable(0x90000000));
                systemInfoPop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.share_app:
                View shareApp = getLayoutInflater().inflate(R.layout.pop_share, null);
                shareInfoPop = new PopupWindow(shareApp,LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                shareInfoPop.setFocusable(true);
                ImageView wx_share = shareApp.findViewById(R.id.wx_share);
                wx_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (weixinapi.getWXAppSupportAPI() >= TIMELINE_SUPPORTED_VERSION){
                            WXWebpageObject webpage = new WXWebpageObject();
                            webpage.webpageUrl = "www.mkono.cn";
                            WXMediaMessage msg = new WXMediaMessage(webpage);
                            msg.title = "迈刻智控APP";
                            msg.description = getString(R.string.app_describe);
                            Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
                            Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, 150, 150, true);
                            thumb.recycle();
                            msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.transaction = buildTransaction("webpage");
                            req.message = msg;
                            req.scene = SendMessageToWX.Req.WXSceneSession;
                            weixinapi.sendReq(req);

                        } else {
                            Toast.makeText(getActivity(),"您的微信版本低或您没有安装微信",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                ImageView wx_moment = shareApp.findViewById(R.id.wx_moment);
                wx_moment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (weixinapi.getWXAppSupportAPI() >= TIMELINE_SUPPORTED_VERSION){
                            WXWebpageObject webpage = new WXWebpageObject();
                            webpage.webpageUrl = "www.mkono.cn";
                            WXMediaMessage msg = new WXMediaMessage(webpage);
                            msg.title = "迈刻智控APP";
                            msg.description = getString(R.string.app_describe);
                            Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
                            Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, 150, 150, true);
                            thumb.recycle();
                            msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.transaction = buildTransaction("webpage");
                            req.message = msg;
                            req.scene = SendMessageToWX.Req.WXSceneTimeline;
                            weixinapi.sendReq(req);

                        } else {
                            Toast.makeText(getActivity(),"您的微信版本低或您没有安装微信",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                shareApp.setOnClickListener(new View.OnClickListener() {//设置点击消失
                    @Override
                    public void onClick(View view) {
                        dissmisspopwindow();
                    }
                });
                shareInfoPop.setBackgroundDrawable(new ColorDrawable(0x90000000));
                shareInfoPop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.user_agree:
                Intent agree = new Intent();
                agree.setClass(getActivity(),UserAgreeAvtivity.class);
                startActivity(agree);
                break;
            case R.id.privacy_policy:
                Intent policy = new Intent();
                policy.setClass(getActivity(),UserPolicyAvtivity.class);
                startActivity(policy);
                break;
        }
    }

    /**
     * 取消popWindow弹窗
     */
    private void dissmisspopwindow() {
        if (systemInfoPop != null) {
            systemInfoPop.dismiss();
        }
        if(shareInfoPop !=null){
            shareInfoPop.dismiss();
        }
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
